package com.video.web.controller.video;

import com.video.common.constant.Constants;
import com.video.common.core.domain.entity.SysUser;
import com.video.common.utils.SecurityUtils;
import com.video.common.utils.sign.Md5Utils;
import com.video.video.domain.vo.UserQuery;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.video.common.annotation.Log;
import com.video.common.core.controller.BaseController;
import com.video.common.core.domain.AjaxResult;
import com.video.common.enums.BusinessType;
import com.video.video.domain.DUser;
import com.video.video.service.IDUserService;
import com.video.common.utils.poi.ExcelUtil;
import com.video.common.core.page.TableDataInfo;

/**
 * 客户Controller
 *
 * @author ruoyi
 * @date 2022-09-27
 */
@RestController
@RequestMapping("/video/DUser")
public class DUserController extends BaseController
{
    @Autowired
    private IDUserService dUserService;

    /**
     * 查询客户列表
     */
    @PreAuthorize("@ss.hasPermi('video:DUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserQuery dUser)
    {
        startPage();
        List<DUser> list = dUserService.selectDUserList(dUser);
        return getDataTable(list);
    }

    /**
     * 导出客户列表
     */
    @PreAuthorize("@ss.hasPermi('video:DUser:export')")
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserQuery dUser)
    {
        List<DUser> list = dUserService.selectDUserList(dUser);
        ExcelUtil<DUser> util = new ExcelUtil<DUser>(DUser.class);
        util.exportExcel(response, list, "客户数据");
    }

    /**
     * 获取客户详细信息
     */
    @PreAuthorize("@ss.hasPermi('video:DUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(dUserService.selectDUserById(id));
    }

    /**
     * 新增客户
     */
    @PreAuthorize("@ss.hasPermi('video:DUser:add')")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DUser dUser)
    {
        Long userId = getLoginUser().getUserId();
        dUser.setCreateBy(String.valueOf(userId));
        return toAjax(dUserService.insertDUser(dUser));
    }

    /**
     * 修改客户
     */
    @PreAuthorize("@ss.hasPermi('video:DUser:edit')")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DUser dUser)
    {
        return toAjax(dUserService.updateDUser(dUser));
    }

    /**
     * 重置密码
     */
    @PreAuthorize("@ss.hasPermi('video:DUser:resetPwd')")
    @Log(title = "客户重置密码", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody DUser dUser)
    {
        DUser user = new DUser();
        user.setId(dUser.getId());
        String pwd = Md5Utils.hash(dUser.getPassword() + Constants.SALT);
        user.setPassword(pwd);
        return toAjax(dUserService.updateDUserInfo(user));
    }

    /**
     * 删除客户
     */
    @PreAuthorize("@ss.hasPermi('video:DUser:remove')")
    @Log(title = "客户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(dUserService.deleteDUserByIds(ids));
    }
}
